#include <QCoreApplication>
#include <QDebug>

#include "asterixparser.h"

QString applyUnitAndScale(const QVariant &value, qreal scale, const QString &unit)
{
    if (qFuzzyCompare(scale, 1))
        return QString::number(value.toDouble()) + (unit.isEmpty() ? "" : (" "  + unit));
    else
        return QString::number(value.toDouble() * scale, 'f', 10) + (unit.isEmpty() ? "" : (" "  + unit));
}

int main(int argc, char *argv[])
{
    QCoreApplication app(argc, argv);

    //cat021
    /*uchar test[] = {
          0x15, 0x00, 0x35, 0xcb, 0x19, 0x71
        , 0x11, 0xc1, 0x01, 0x04, 0x16, 0x00, 0x11, 0x44, 0x4c, 0x65, 0x80, 0x09, 0xf1, 0x80, 0x2c, 0x25
        , 0xd8, 0x59, 0xe5, 0xff, 0xe0, 0x07, 0x4c, 0x65, 0x80, 0x02, 0x7b, 0x2d, 0x35, 0x08, 0x12, 0x00
        , 0x03, 0x34, 0x81, 0x37, 0xcf, 0x5d, 0xa0, 0x01, 0x07, 0x88, 0x10, 0x01, 0x11, 0x11, 0x02
    };*/

    //cat062
    /*uchar test[] = {
        0x3e, 0x00, 0x2b, 0x19, 0x31, 0x10, 0x47, 0x88, 0xf6, 0x00, 0x56, 0xfe, 0x34, 0x01, 0x27, 0xad,
        0x07, 0x00, 0x60, 0x6c, 0x31, 0x00, 0x00, 0x00, 0xc1, 0x01, 0x32, 0xff, 0xe1, 0x01, 0x60, 0x6c,
        0x31, 0x00, 0x00, 0x00, 0x4e, 0xee, 0x00, 0x93, 0x00, 0x00, 0x00
    };*/

    //cat048
    uchar test[] = {
          0x30, 0x00, 0xbf, 0xf3, 0x01, 0x0d
        , 0x04, 0xe9, 0x0f, 0x2f, 0x6a, 0x6e, 0x21, 0x00
        , 0x2a, 0x81, 0x16, 0xe1, 0x18, 0x3b, 0xa7, 0x02
        , 0xd8, 0x80, 0x00, 0x17, 0x21, 0x05, 0xdd, 0x59
        , 0x87, 0x41, 0x0c, 0x00, 0x00, 0x0b, 0x01, 0x4c
        , 0x2a, 0x84, 0xfd, 0x07, 0x01, 0x04, 0x38, 0x23
        , 0x01, 0x7a, 0x6a, 0x05, 0x3c, 0x07, 0x06, 0x0b
        , 0xd0, 0x00, 0x00, 0x08, 0xd5, 0xff, 0x01, 0x01
        , 0x24, 0xe9, 0x0f, 0x2f, 0x69, 0xad, 0x41, 0x00
        , 0x2a, 0x86, 0x17, 0x09, 0x07, 0x18, 0x02, 0xd0
        , 0xe0, 0xc0, 0x1b, 0xc4, 0x01, 0xf4, 0x0e, 0x05
        , 0x91, 0x41, 0x84, 0x00, 0x00, 0x01, 0x4c, 0x02
        , 0x08, 0x00, 0x9c, 0x84, 0xf3, 0x01, 0x0d, 0x04
        , 0xe9, 0x0f, 0x2f, 0x6a, 0x6d, 0x21, 0x00, 0x1e
        , 0xf1, 0x16, 0x36, 0x18, 0x25, 0xb3, 0x05, 0x41
        , 0x80, 0x00, 0x34, 0x21, 0x05, 0xdd, 0x59, 0x87
        , 0x41, 0x0c, 0x00, 0x00, 0x18, 0x04, 0xbb, 0x1e
        , 0xf3, 0xfc, 0xa8, 0x02, 0x02, 0x23, 0x23, 0x01
        , 0x7a, 0x68, 0x0e, 0x46, 0x06, 0x0c, 0x13, 0xa0
        , 0x00, 0x00, 0x14, 0x47, 0xff, 0x01, 0x01, 0x04
        , 0xe9, 0x0f, 0x2f, 0x69, 0xac, 0x41, 0x00, 0x1e
        , 0xed, 0x16, 0x6e, 0x03, 0xe4, 0x04, 0xe4, 0xe0
        , 0xe5, 0x13, 0xc9, 0x0e, 0x05, 0x91, 0x41, 0x84
        , 0x00, 0x00, 0x04, 0xbb, 0x02, 0x08, 0x00, 0x9c
        , 0x83
    };

    AsterixParser parser(QT_STRINGIFY(PWD_PATH) + QString("/../asterixSpecification"));

    auto mapList = parser.parseToFsnMap(test);

    for (int i = 0; i < mapList.length(); i++) {
        qDebug().noquote().nospace() << "Message #" + QString::number(i + 1).rightJustified(2, '0');
        const auto map = mapList.at(i);
        for (const auto &block: map) {
            qDebug() << "  " << block.frn << block.id << block.name << block.rawValue;
            if (!block.subBlock.isEmpty()) {
                for (const auto &subBlock: block.subBlock)
                    qDebug() << "    "
                             << subBlock.frn
                             << subBlock.id
                             << subBlock.name
                             << subBlock.value
                             << applyUnitAndScale(subBlock.value, subBlock.scale, subBlock.unit);
            }
        }

        //cat021 REF
        if (parser.getCategory(test) == 21 && map.contains(48)) {
            auto ref_map = parser.parseReservedExpansionField(parser.getCategory(test), map[48]);
            for (const auto &ref: qAsConst(ref_map)) {
                for (const auto &subField: ref.subField)
                    qDebug() << "    "
                             << subField.name
                             << (subField.value.size() == 1 ? (parser.getU8(subField.value)) : (parser.getU16(subField.value)));
            }
        }

        qDebug() << Qt::endl;
    }


    return app.exec();
}
